package de.tud.stg.emergent.policy.client;

import java.rmi.RemoteException;
import java.util.Properties;

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.neethi.Policy;
import org.apache.rampart.RampartMessageData;
import org.apache.rampart.policy.model.CryptoConfig;
import org.apache.rampart.policy.model.RampartConfig;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

import de.tud.stg.emergent.policy.utils.CommonPolicyUtils;

public class RampartClient {

	private PolicyServiceStub stub;

	public RampartClient() {
		
		this("http://localhost:8080/axis2/services/PolicyService");

	}
	
	private String user;
	private String password;
	
	public String getUser() {
		return user;
	}

	private void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	private void setPassword(String password) {
		this.password = password;
	}

	public RampartClient(String user, String password){
		this.setUser(user);
		this.setPassword(password);
	}
	
	
	public RampartClient(String url) {
		try {
			// get Modulrepository
			ConfigurationContext configContext;

			configContext = ConfigurationContextFactory
					.createConfigurationContextFromFileSystem(CommonPolicyUtils.rampartRepositoryPath, CommonPolicyUtils.rampartRepositoryPath+"\\conf\\client.axis2.xml");
							
			// create new Stub
			stub = new PolicyServiceStub(configContext, url);

			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public PolicyServiceStub setPasswordClient(String user, String password) throws AxisFault {
		
		
		this.setUser(user);
		this.setPassword(password);
		
		// configure and engage Rampart
		ServiceClient client = stub._getServiceClient();
		Options options = client.getOptions();


		options.setUserName(this.getUser());
		options.setPassword(this.getPassword());

		
		
		
		
//		
//		
//		 RampartConfig rampartConfig = new RampartConfig();
//		  rampartConfig.setUser(this.getUser());
//		  rampartConfig.setPwCbClass("de.tud.stg.emergent.policy.rampart.PWCBHandler");
////			Policy policy = CommonPolicyUtils
////			.getNeethiPolicyFromFile(".\\src\\main\\java\\de\\tud\\stg\\emergent\\policy\\rampart\\policy1.xml");
//
//		  Policy tpolicy = new Policy();
//		  tpolicy.addAssertion(rampartConfig);
//		  
//		  
//		  options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, tpolicy);
		
		  
		  client.setOptions(options);
		client.engageModule("addressing");
		client.engageModule("rampart");
		stub._setServiceClient(client);
		
		
		return stub;
		// client.getAxisService().getPolicySubject().attachPolicy(policy);
		// options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,
		// policy);


		/*
		<ramp:RampartConfig xmlns:ramp="http://ws.apache.org/rampart/policy">
		<ramp:user>service</ramp:user>
		<ramp:passwordCallbackClass>tutorial.rampart.service.PWCBHandler</ramp:passwordCallbackClass>
		<ramp:signatureCrypto>
		    <ramp:crypto provider="org.apache.ws.security.components.crypto.Merlin">
		        <ramp:property name="org.apache.ws.security.crypto.merlin.keystore.type">JKS</ramp:property>
		        <ramp:property name="org.apache.ws.security.crypto.merlin.file">path/to/service.jks</ramp:property>
		        <ramp:property name="org.apache.ws.security.crypto.merlin.keystore.password">servicePW</ramp:property>
		    </ramp:crypto>
		</ramp:signatureCrypto>
</ramp:RampartConfig>
		*/
		
		/*
		  RampartConfig rampartConfig = new RampartConfig();
		  rampartConfig.setUser("client");
		  rampartConfig.setPwCbClass("tutorial.rampart.client.PWCBHandler");

		  CryptoConfig sigCrypto = new CryptoConfig();
		  sigCrypto.setProvider("org.apache.ws.security.components.crypto.Merlin");

		  Properties props = new Properties();
		  props.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
		  props.setProperty("org.apache.ws.security.crypto.merlin.file","keys/client.jks");
		  props.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "clientPW");

		  sigCrypto.setProp(props);

		  rampartConfig.setSigCryptoConfig(sigCrypto);

		  Policy tpolicy = new Policy();
		  tpolicy.addAssertion(rampartConfig);
		*/
	}

	public PolicyServiceStub getStub(){
		return stub;
	}
}